package com.zyl.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyl.enums.IsReadEnum;
import com.zyl.pojo.CommentRecord;
import com.zyl.pojo.Page;
import com.zyl.service.ArticleService;
import com.zyl.service.CommentRecordService;
import com.zyl.util.ResultVoUtil;
import com.zyl.vo.AdminArticleVo;
import com.zyl.vo.ArticleVo;
import com.zyl.vo.DataVo;
import com.zyl.vo.ResultVo;
import org.apache.ibatis.io.ResolverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: Zyl
 * @Param:
 * @return:
 * @Description:
 */
@RestController
public class Article {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentRecordService commentRecordService;
    @PostMapping("/article")
    public ResultVo findAllArticle(@RequestBody Page page){
        PageHelper.offsetPage((page.getCurrentPage() - 1) * page.getPageSize(),page.getPageSize());
        ArticleVo list = articleService.findAll();
        List<ArticleVo> articleVos=new ArrayList<>();
        articleVos.add(list);
        PageInfo<ArticleVo> articleVo=new PageInfo<>(articleVos);
        articleVo.setTotal(articleService.articleCount());
        return ResultVoUtil.success(articleVo);
    }
    @GetMapping("/article/{articleId}")
    public ResultVo findArticleByArticleId(@PathVariable("articleId") Long articleId){
        AdminArticleVo articleByArticleId = articleService.findArticleByArticleId(articleId);
        return ResultVoUtil.success(articleByArticleId);
    }
    @GetMapping("/findComment/{articleId}")
    public ResultVo findCommentByArticleId(@PathVariable("articleId") Long articleId){
        DataVo commentByArticleId = commentRecordService.findCommentByArticleId(articleId);
        return ResultVoUtil.success(commentByArticleId);
    }
    @PostMapping("/addComment")
    public ResultVo addComment(@RequestBody CommentRecord commentRecord){
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss ");
        Date date = new Date(System.currentTimeMillis());
        commentRecord.setIsRead(0);
        commentRecord.setCommentDate(formatter.format(date));
        commentRecordService.addComment(commentRecord);
        return ResultVoUtil.success(null);
    }
    @PostMapping("/addReply")
    public ResultVo addReply(@RequestBody CommentRecord commentRecord){
        commentRecord.setIsRead(0);
        commentRecordService.addReply(commentRecord);
        return ResultVoUtil.success(null);
    }
}
